package com.guanjia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guanjia.entity.Bussiness;
import com.guanjia.entity.EnterpriseInformation;
import com.guanjia.entity.User;
import com.guanjia.entity.UserInformation;
import com.guanjia.entity.vo.BussinessVo;
import com.guanjia.mapper.BussinessMapper;
import com.guanjia.mapper.UserMapper;
import com.guanjia.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    public static Integer CURRENT_PAGE = 1;
    public static Integer PAGE_SIZE = 2;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BussinessMapper bussinessMapper;
    @Bean
    PasswordEncoder getPassword(){
        return new BCryptPasswordEncoder();
    }



    @Override
    public String regUser(User user) {
        PasswordEncoder p=getPassword();
        //查询该手机号是否被注册
      int i=  userMapper.selectCount(new QueryWrapper<User>().eq("tell",user.getTell()));
      if (i==0){
          String pass=user.getPassword();
          user.setPassword(p.encode(pass));
          userMapper.insert(user);
          return "1";

      }
        return "0"; //账户存在 返回0 失败

    }

    @Override
    public String isnull(User user) {

        //查询该手机号是否被注册
        int i=  userMapper.selectCount(new QueryWrapper<User>().eq("tell",user.getTell()));
        if (i==0){

            return "1";

        }
        return "0"; //账户存在 返回0 失败

    }

    @Override
    public void updataPwd(User user) {
        User user1 = new User();
        user1.setId(user.getId());
        String pwd = new BCryptPasswordEncoder().encode(user.getPassword());
        user1.setPassword(pwd);
       // System.out.println(user1);
        userMapper.updateById(user1);
    }

    @Override
    public void updataTell(User user) {
        User user1 = new User();
        user1.setId(user.getId());
        user1.setTell(user.getTell());
        // System.out.println(user1);
        userMapper.updateById(user1);
    }

    @Override
    public void updatamoney(User user) {
        User user1 = new User();
        user1.setId(user.getId());
        user1.setBalance(user.getBalance());
        // System.out.println(user1);
        userMapper.updateById(user1);
    }

    @Override
    public void updateenterInfo(EnterpriseInformation enterpriseInformation) {
        System.out.println(enterpriseInformation);
       // EnterpriseInformation enterpriseInformation1 = userMapper.findUserEnterInfoByUserId(enterpriseInformation.getUserId());
        userMapper.updateenterInfo(enterpriseInformation);

    }

    @Override
    public List<Bussiness> findUsertransactionByUserId(Integer id) {

        return userMapper.findUsertransactionByUserId(id);
    }

    @Override
    public List<Bussiness> findUsertransactionInfoByUserId(BussinessVo bussinessVo) {
        return userMapper.findUsertransactionInfoByUserId(bussinessVo);
    }

    @Override
    public IPage<Bussiness> findPageList(Integer page, Integer size, String group, String start, String end,Integer userId) {
        if (page==null){
            page=1;
        }
        if (size==null){
            size=2;
        }

            if (page != null) {
                CURRENT_PAGE = page;
            }
            if (size != null) {
                PAGE_SIZE = size;
            }
        Map<String ,Object> map= new HashMap<String ,Object>();
        if (group!=null){
        if (group.equals("null")){
            group=null;
        }}
            map.put("type",group);
            QueryWrapper<Bussiness> bussinessQueryWrapper= new QueryWrapper<Bussiness>().eq("userId",userId).allEq(map,false);
            if(start!=null&&!start.equals("null")&&!start.equals("")){
            bussinessQueryWrapper=new QueryWrapper<Bussiness>().eq("userId",userId).allEq(map,false).ge("createtime",start);
            if (end!=null&&!end.equals("null")&&!end.equals("")){
                bussinessQueryWrapper=new QueryWrapper<Bussiness>().eq("userId",userId).allEq(map,false).ge("createtime",start).le("createtime",end);
            }
            }
        if (start==null&&(end!=null&&!end.equals("null")&&!equals(""))){
            bussinessQueryWrapper=new QueryWrapper<Bussiness>().eq("userId",userId).allEq(map,false).ge("createtime",start).le("createtime",end);
        }
            IPage<Bussiness> pageFilm = bussinessMapper.selectPage(
                    new Page<Bussiness>(CURRENT_PAGE, PAGE_SIZE),
                   bussinessQueryWrapper);
            return pageFilm;


    }

    @Override
    public UserInformation findUserNameById(Integer id) {
        return userMapper.findUserNameById(id);
    }


}
